package com.design.classroommanagement

import com.alibaba.android.arouter.facade.annotation.Route
import com.design.classroommanagement.api.getDao
import com.design.classroommanagement.databinding.FragmentUpdatePasswordBinding
import com.design.classroommanagement.manager.UserAccountManager
import com.laughing.lib.base.BaseFragmentNoVM
import com.laughing.lib.utils.clickWithTrigger
import com.laughing.lib.utils.runInBg
import com.laughing.lib.utils.showToast
import kotlinx.android.synthetic.main.fragment_update_password.*


@Route(path = Pages.UpdatePasswordFragment)
class UpdatePasswordFragment : BaseFragmentNoVM<FragmentUpdatePasswordBinding>() {

    override fun initCompleted() {
        super.initCompleted()
        binding.nav.titleTv.text = "修改密码"
        binding.nav.backIv.clickWithTrigger {
            finish()
        }
        binding.submitBtn.clickWithTrigger {
            update()
        }
    }

    private fun update(){
        val pwd = pwdEt.text.toString()
        val pwd1 = pwdEt1.text.toString()
        val pwd2 = pwdEt2.text.toString()

        val user = UserAccountManager.user ?: return

        if (user.password != pwd) {
            showToast("旧密码错误")
            return
        }

        if (pwd1 != pwd2) {
            return showToast("请确认新密码")
        }

        runInBg {
            user.password = pwd1
            user.role.getDao().update(user)
            showToast("修改成功")
            finish()
        }
    }
}